import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isUndefined from "lodash/isUndefined";
var _excluded = ["startDate", "endDate", "retryNumber"];

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _class, _class2;

import React from 'react';
import { Button, Col, DatePicker, Divider, Form, Input, InputNumber, Modal, Row, Select, Spin } from 'antd';
import { Bind } from 'lodash-decorators';
import moment from 'moment';
import Lov from 'components/Lov';
import Switch from 'components/Switch';
import { getDateFormat } from 'utils/utils';
import intl from 'utils/intl';
import { DEFAULT_DATETIME_FORMAT } from 'utils/constants';
import { CODE_UPPER, EMAIL } from 'utils/regExp';
import CronModal from "./CronModal";
var dateFormat = getDateFormat();
var Option = Select.Option;
var FormItem = Form.Item;
var formLayout = {
  labelCol: {
    span: 5
  },
  wrapperCol: {
    span: 15
  }
};
var Drawer = (_dec = Form.create({
  fieldNameProp: null
}), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec9 = Bind(), _dec10 = Bind(), _dec(_class = (_class2 = /*#__PURE__*/function (_React$PureComponent) {
  _inherits(Drawer, _React$PureComponent);

  var _super = _createSuper(Drawer);

  function Drawer(props) {
    var _this;

    _classCallCheck(this, Drawer);

    _this = _super.call(this, props);
    _this.state = {
      cronVisible: false,
      currentJobCorn: ''
    };
    return _this;
  }

  _createClass(Drawer, [{
    key: "handleOk",
    value: function handleOk() {
      var _this$props = this.props,
          form = _this$props.form,
          _this$props$executorC = _this$props.executorConfigList,
          executorConfigList = _this$props$executorC === void 0 ? [] : _this$props$executorC,
          _this$props$onOk = _this$props.onOk,
          onOk = _this$props$onOk === void 0 ? function (e) {
        return e;
      } : _this$props$onOk;
      form.validateFields(function (err, fieldsValue) {
        if (!err) {
          var startDate = fieldsValue.startDate,
              endDate = fieldsValue.endDate,
              retryNumber = fieldsValue.retryNumber,
              others = _objectWithoutProperties(fieldsValue, _excluded);

          var params = _objectSpread({
            strategyParam: {
              retryNumber: retryNumber
            },
            startDate: startDate ? moment(startDate).format(DEFAULT_DATETIME_FORMAT) : null,
            endDate: endDate ? moment(endDate).format(DEFAULT_DATETIME_FORMAT) : null
          }, others);

          if (executorConfigList.length > 0) {
            var strategyParam = {
              jobWeight: {}
            };
            executorConfigList.forEach(function (item, index) {
              Object.assign(strategyParam, {
                jobWeight: _objectSpread(_objectSpread({}, strategyParam.jobWeight), {}, _defineProperty({}, "".concat(item.address), fieldsValue["".concat(item.configId, "/").concat(index)]))
              });
              delete params["".concat(item.configId, "/").concat(index)];
            });

            if (fieldsValue.failStrategy === 'RETRY' && fieldsValue.retryNumber !== undefined) {
              Object.assign(strategyParam, {
                retryNumber: fieldsValue.retryNumber
              });
            }

            Object.assign(params, {
              strategyParam: strategyParam
            });
          }

          onOk(params);
        }
      });
    }
  }, {
    key: "handleCheck",
    value: function handleCheck() {
      var _this$props2 = this.props,
          _this$props2$onCheck = _this$props2.onCheck,
          onCheck = _this$props2$onCheck === void 0 ? function (e) {
        return e;
      } : _this$props2$onCheck,
          form = _this$props2.form;
      var executorId = form.getFieldValue('executorId');
      onCheck(executorId);
    }
  }, {
    key: "isJson",
    value: function isJson(string) {
      try {
        if (typeof JSON.parse(string) === 'object') {
          return true;
        }
      } catch (e) {
        return false;
      }
    }
  }, {
    key: "renderConfigList",
    value: function renderConfigList() {
      var _this$props3 = this.props,
          form = _this$props3.form,
          _this$props3$configLo = _this$props3.configLoading,
          configLoading = _this$props3$configLo === void 0 ? false : _this$props3$configLo,
          _this$props3$executor = _this$props3.executorConfigList,
          executorConfigList = _this$props3$executor === void 0 ? [] : _this$props3$executor;
      var getFieldDecorator = form.getFieldDecorator;
      return /*#__PURE__*/React.createElement(Spin, {
        spinning: configLoading
      }, /*#__PURE__*/React.createElement(Divider, {
        orientation: "left"
      }, intl.get('csdr.jobInfo.model.jobInfo.actuator').d('执行器配置列表')), /*#__PURE__*/React.createElement(Row, null, executorConfigList.map(function (item, index) {
        var weight = item.weight,
            address = item.address,
            configId = item.configId;
        return /*#__PURE__*/React.createElement(Col, {
          span: 12,
          key: configId
        }, /*#__PURE__*/React.createElement(FormItem, {
          labelCol: {
            span: 9
          },
          wrapperCol: {
            span: 15
          },
          label: "".concat(address)
        }, getFieldDecorator("".concat(configId, "/").concat(index), {
          initialValue: weight
        })( /*#__PURE__*/React.createElement(InputNumber, null))));
      })));
    }
    /**
     * 监听执行器策略选择
     * @param {string} data - 执行器策略
     */

  }, {
    key: "executorStrategyConfig",
    value: function executorStrategyConfig(data) {
      var _this$props4 = this.props,
          form = _this$props4.form,
          _this$props4$initData = _this$props4.initData,
          initData = _this$props4$initData === void 0 ? {} : _this$props4$initData,
          _this$props4$onConfig = _this$props4.onConfig,
          onConfig = _this$props4$onConfig === void 0 ? function (e) {
        return e;
      } : _this$props4$onConfig;
      var executorId = initData.executorId;
      var strategy = data;

      if (strategy === 'JOB_WEIGHT') {
        if (initData.executorStrategy === 'JOB_WEIGHT' && executorId === form.getFieldValue('executorId') && strategy === 'JOB_WEIGHT') {
          onConfig(initData);
        } else {
          onConfig({
            executorId: form.getFieldValue('executorId')
          });
        }
      } else {
        onConfig();
      }
    }
  }, {
    key: "executorChange",
    value: function executorChange(record, data) {
      var _this$props5 = this.props,
          form = _this$props5.form,
          _this$props5$onConfig = _this$props5.onConfig,
          onConfig = _this$props5$onConfig === void 0 ? function (e) {
        return e;
      } : _this$props5$onConfig;
      var executorStrategy = form.getFieldValue('executorStrategy');

      if (data !== undefined && executorStrategy === 'JOB_WEIGHT') {
        onConfig(data);
      } else {
        onConfig();
      }
    }
  }, {
    key: "showCronModal",
    value: function showCronModal() {
      var form = this.props.form;
      this.setState({
        cronVisible: true
      });

      var _form$getFieldsValue = form.getFieldsValue(),
          jobCron = _form$getFieldsValue.jobCron;

      this.setState({
        currentJobCorn: jobCron
      });
    }
  }, {
    key: "handleCronCancel",
    value: function handleCronCancel() {
      this.setState({
        cronVisible: false
      });
    }
  }, {
    key: "handleCronOk",
    value: function handleCronOk(value) {
      var form = this.props.form;
      form.setFieldsValue({
        jobCron: value
      });
      this.setState({
        cronVisible: false
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props6 = this.props,
          form = _this$props6.form,
          _this$props6$initData = _this$props6.initData,
          initData = _this$props6$initData === void 0 ? {} : _this$props6$initData,
          jobInfo = _this$props6.jobInfo,
          title = _this$props6.title,
          modalVisible = _this$props6.modalVisible,
          loading = _this$props6.loading,
          onCancel = _this$props6.onCancel,
          currentOrganizationId = _this$props6.tenantId,
          tenantRoleLevel = _this$props6.tenantRoleLevel,
          _this$props6$checkLoa = _this$props6.checkLoading,
          checkLoading = _this$props6$checkLoa === void 0 ? false : _this$props6$checkLoa,
          _this$props6$detailLo = _this$props6.detailLoading,
          detailLoading = _this$props6$detailLo === void 0 ? false : _this$props6$detailLo,
          _this$props6$executor = _this$props6.executorConfigList,
          executorConfigList = _this$props6$executor === void 0 ? [] : _this$props6$executor;
      var _this$state = this.state,
          cronVisible = _this$state.cronVisible,
          currentJobCorn = _this$state.currentJobCorn;
      var getFieldDecorator = form.getFieldDecorator;
      var _jobInfo$executorRout = jobInfo.executorRouteList,
          executorRouteList = _jobInfo$executorRout === void 0 ? [] : _jobInfo$executorRout,
          _jobInfo$glueTypeList = jobInfo.glueTypeList,
          glueTypeList = _jobInfo$glueTypeList === void 0 ? [] : _jobInfo$glueTypeList,
          _jobInfo$executorFail = jobInfo.executorFailList,
          executorFailList = _jobInfo$executorFail === void 0 ? [] : _jobInfo$executorFail;
      var executorName = initData.executorName,
          executorId = initData.executorId,
          executorStrategy = initData.executorStrategy,
          strategyParam = initData.strategyParam,
          jobParam = initData.jobParam,
          jobHandler = initData.jobHandler,
          glueType = initData.glueType,
          description = initData.description,
          jobCron = initData.jobCron,
          jobCode = initData.jobCode,
          failStrategy = initData.failStrategy,
          alarmEmail = initData.alarmEmail,
          tenantId = initData.tenantId,
          tenantName = initData.tenantName,
          jobId = initData.jobId,
          _initData$cycleFlag = initData.cycleFlag,
          cycleFlag = _initData$cycleFlag === void 0 ? 1 : _initData$cycleFlag,
          _initData$serial = initData.serial,
          serial = _initData$serial === void 0 ? 0 : _initData$serial,
          _initData$initFlag = initData.initFlag,
          initFlag = _initData$initFlag === void 0 ? 0 : _initData$initFlag;
      var cronModalProps = {
        visible: cronVisible,
        onCancel: this.handleCronCancel,
        onOk: this.handleCronOk,
        initialValue: currentJobCorn
      };
      return /*#__PURE__*/React.createElement(Modal, {
        destroyOnClose: true,
        title: title,
        width: "1000px",
        visible: modalVisible,
        wrapClassName: "ant-modal-sidebar-right",
        transitionName: "move-right",
        onCancel: onCancel,
        footer: [/*#__PURE__*/React.createElement(Button, {
          key: "cancel",
          onClick: onCancel
        }, intl.get('cpaas.common.button.cancel').d('取消')), /*#__PURE__*/React.createElement(Button, {
          disabled: _isUndefined(form.getFieldValue('executorId')),
          key: "check",
          onClick: this.handleCheck,
          loading: checkLoading
        }, intl.get('csdr.jobInfo.view.button.check').d('校验执行器')), /*#__PURE__*/React.createElement(Button, {
          type: "primary",
          key: "save",
          onClick: this.handleOk,
          loading: loading
        }, intl.get('cpaas.common.button.ok').d('确定'))]
      }, /*#__PURE__*/React.createElement(Spin, {
        spinning: detailLoading
      }, /*#__PURE__*/React.createElement(Form, null, /*#__PURE__*/React.createElement(Row, null, /*#__PURE__*/React.createElement(Col, {
        span: 12
      }, /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('csdr.jobInfo.model.jobInfo.jobCode').d('任务编码')
      }), getFieldDecorator('jobCode', {
        initialValue: jobCode,
        rules: [{
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('csdr.jobInfo.model.jobInfo.jobCode').d('任务编码')
          })
        }, {
          pattern: CODE_UPPER,
          message: intl.get('cpaas.common.validation.codeUpper').d('全大写及数字，必须以字母、数字开头，可包含“-”、“_”、“.”、“/”')
        }]
      })( /*#__PURE__*/React.createElement(Input, {
        trim: true,
        typeCase: "upper",
        inputChinese: false,
        disabled: jobId !== undefined
      }))), !tenantRoleLevel && /*#__PURE__*/React.createElement(Form.Item, Object.assign({}, formLayout, {
        label: intl.get('csdr.jobInfo.model.jobInfo.tenantName').d('所属租户')
      }), getFieldDecorator('tenantId', {
        initialValue: tenantId,
        rules: [{
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('csdr.jobInfo.model.jobInfo.tenantName').d('所属租户')
          })
        }]
      })( /*#__PURE__*/React.createElement(Lov, {
        code: "CPFM.TENANT",
        textValue: tenantName,
        textField: "tenantName",
        allowClear: false,
        disabled: jobId !== undefined
      }))), /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('csdr.jobInfo.model.jobInfo.executorFailStrategy').d('失败处理策略')
      }), getFieldDecorator('failStrategy', {
        initialValue: failStrategy,
        rules: [{
          type: 'string',
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('csdr.jobInfo.model.jobInfo.executorFailStrategy').d('失败处理策略')
          })
        }]
      })( /*#__PURE__*/React.createElement(Select, {
        style: {
          width: '100%'
        }
      }, executorFailList.map(function (item) {
        return /*#__PURE__*/React.createElement(Option, {
          label: item.meaning,
          value: item.value,
          key: item.value
        }, item.meaning);
      })))), /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('csdr.jobInfo.model.jobInfo.groupId').d('执行器')
      }), getFieldDecorator('executorId', {
        initialValue: executorId,
        rules: [{
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('csdr.jobInfo.model.jobInfo.groupId').d('执行器')
          })
        }]
      })( /*#__PURE__*/React.createElement(Lov, {
        key: executorName,
        allowClear: false,
        code: "CSDR.AVAIL_EXECUTOR",
        onChange: this.executorChange,
        textValue: executorName,
        queryParams: {
          tenantId: currentOrganizationId
        }
      }))), /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('csdr.jobInfo.model.jobInfo.executorStrategy').d('执行器策略')
      }), getFieldDecorator('executorStrategy', {
        initialValue: executorStrategy,
        rules: [{
          type: 'string',
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('csdr.jobInfo.model.jobInfo.executorStrategy').d('执行器策略')
          })
        }]
      })( /*#__PURE__*/React.createElement(Select, {
        style: {
          width: '100%'
        },
        onChange: this.executorStrategyConfig,
        disabled: form.getFieldValue('executorId') === undefined
      }, executorRouteList.map(function (item) {
        return /*#__PURE__*/React.createElement(Option, {
          label: item.meaning,
          value: item.value,
          key: item.value
        }, item.meaning);
      })))), /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: "JobHandler"
      }), getFieldDecorator('jobHandler', {
        rules: [{
          required: form.getFieldValue('glueType') === 'SIMPLE',
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('csdr.jobInfo.model.jobInfo.jobHandler').d('JobHandler')
          })
        }],
        initialValue: jobHandler
      })( /*#__PURE__*/React.createElement(Input, null))), /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('csdr.jobInfo.model.jobInfo.executorParam').d('任务参数')
      }), getFieldDecorator('jobParam', {
        initialValue: jobParam,
        rules: [{
          validator: function validator(rule, value, callback) {
            if (!value || _this2.isJson(value)) {
              callback();
            } else {
              callback(intl.get('csdr.jobInfo.view.validate.additionalInformation').d('请输入正确的json字符串'));
            }
          }
        }]
      })( /*#__PURE__*/React.createElement(Input, null))), /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('csdr.jobInfo.model.jobInfo.serial').d('串行任务')
      }), getFieldDecorator('serial', {
        initialValue: serial
      })( /*#__PURE__*/React.createElement(Switch, null)))), /*#__PURE__*/React.createElement(Col, {
        span: 12
      }, /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('csdr.jobInfo.model.jobInfo.jobDesc').d('任务描述')
      }), getFieldDecorator('description', {
        initialValue: description
      })( /*#__PURE__*/React.createElement(Input, null))), /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('csdr.jobInfo.model.jobInfo.glueType').d('任务类型')
      }), getFieldDecorator('glueType', {
        initialValue: glueType,
        rules: [{
          type: 'string',
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('csdr.jobInfo.model.jobInfo.glueType').d('任务类型')
          })
        }]
      })( /*#__PURE__*/React.createElement(Select, {
        style: {
          width: '100%'
        }
      }, glueTypeList.map(function (item) {
        return /*#__PURE__*/React.createElement(Option, {
          label: item.meaning,
          value: item.value,
          key: item.value
        }, item.meaning);
      })))), form.getFieldValue('failStrategy') === 'RETRY' && /*#__PURE__*/React.createElement(FormItem, Object.assign({
        label: intl.get('csdr.jobInfo.model.jobInfo.retryNumber').d('重试次数')
      }, formLayout), getFieldDecorator('retryNumber', {
        initialValue: strategyParam && JSON.parse(strategyParam).retryNumber
      })( /*#__PURE__*/React.createElement(InputNumber, {
        min: 0,
        step: 1,
        style: {
          width: '100%'
        }
      }))), /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('csdr.jobInfo.model.jobInfo.jobCron').d('Cron')
      }), getFieldDecorator('jobCron', {
        initialValue: jobCron
      })( /*#__PURE__*/React.createElement(Input, {
        inputChinese: false,
        onClick: this.showCronModal
      }))), /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('csdr.jobInfo.model.jobInfo.alarmEmail').d('报警邮件')
      }), getFieldDecorator('alarmEmail', {
        initialValue: alarmEmail,
        rules: [{
          pattern: EMAIL,
          message: intl.get('cpaas.common.validation.email').d('邮箱格式不正确')
        }]
      })( /*#__PURE__*/React.createElement(Input, null))), /*#__PURE__*/React.createElement(FormItem, Object.assign({
        label: intl.get('csdr.jobInfo.model.jobInfo.startDate').d('有效时间从')
      }, formLayout), getFieldDecorator('startDate', {
        initialValue: initData.startDate && moment(initData.startDate, "".concat(dateFormat, " HH:mm:ss"))
      })( /*#__PURE__*/React.createElement(DatePicker, {
        style: {
          width: '100%'
        },
        showTime: true,
        format: "".concat(dateFormat, " HH:mm:ss"),
        placeholder: "",
        disabledDate: function disabledDate(currentDate) {
          return form.getFieldValue('endDate') && moment(form.getFieldValue('endDate')).isBefore(currentDate, 'day');
        }
      }))), /*#__PURE__*/React.createElement(FormItem, Object.assign({
        label: intl.get('csdr.jobInfo.model.jobInfo.endDate').d('有效时间至')
      }, formLayout), getFieldDecorator('endDate', {
        initialValue: initData.endDate && moment(initData.endDate, "".concat(dateFormat, " HH:mm:ss"))
      })( /*#__PURE__*/React.createElement(DatePicker, {
        style: {
          width: '100%'
        },
        showTime: true,
        format: "".concat(dateFormat, " HH:mm:ss"),
        placeholder: "",
        disabledDate: function disabledDate(currentDate) {
          return form.getFieldValue('startDate') && moment(form.getFieldValue('startDate')).isAfter(currentDate, 'day');
        }
      }))), /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('csdr.jobInfo.model.jobInfo.cycleFlag').d('周期性')
      }), getFieldDecorator('cycleFlag', {
        initialValue: cycleFlag
      })( /*#__PURE__*/React.createElement(Switch, null))), /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('csdr.jobInfo.model.jobInfo.initFlag').d('自动初始化')
      }), getFieldDecorator('initFlag', {
        initialValue: initFlag
      })( /*#__PURE__*/React.createElement(Switch, null))))), executorConfigList.length > 0 ? this.renderConfigList() : null)), /*#__PURE__*/React.createElement(CronModal, cronModalProps));
    }
  }]);

  return Drawer;
}(React.PureComponent), (_applyDecoratedDescriptor(_class2.prototype, "handleOk", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "handleOk"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleCheck", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "handleCheck"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "isJson", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "isJson"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "renderConfigList", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "renderConfigList"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "executorStrategyConfig", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "executorStrategyConfig"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "executorChange", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "executorChange"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "showCronModal", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "showCronModal"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleCronCancel", [_dec9], Object.getOwnPropertyDescriptor(_class2.prototype, "handleCronCancel"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleCronOk", [_dec10], Object.getOwnPropertyDescriptor(_class2.prototype, "handleCronOk"), _class2.prototype)), _class2)) || _class);
export { Drawer as default };